﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;
using Facebook.Services;
using Facebook.Contracts;
namespace WebApplication1
{
    public partial class Search : System.Web.UI.Page
    {

        IFacebookService facebookservice;

        protected void Page_Load(object sender, EventArgs e)
        {

            facebookservice = this.Session["facebookservice"] as IFacebookService;

            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            listOfSearchResults.ItemDataBound += new EventHandler<ListViewItemEventArgs>(listOfSearchResults_ItemDataBound);
            next.Click += new EventHandler(next_Click);

            previous.Click += new EventHandler(previous_Click);
           
        }

        void previous_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            if (lb != null)
            {
                  FacebookEntity result = facebookservice.GetPage(lb.CommandArgument) as FacebookEntity;
                  BindResultPage(result);
            }
        }

        void next_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            if (lb != null) 
            {
                FacebookEntity result = facebookservice.GetPage(lb.CommandArgument) as FacebookEntity;
                BindResultPage(result);
            }
        }

        void listOfSearchResults_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewItem lvi = e.Item;
            
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            if (facebookservice != null)
            {
                SearchType searchType;
                 bool isEnumType = Enum.TryParse <SearchType>(this.ddlsearhType.SelectedItem.Text, false, out searchType);

                 if (!isEnumType)
                 {
                     searchType = SearchType.events;
                 }
                                
                 //List<Dictionary<string, object>> listOfResults = (result as FBObject).Dictionary["data"] as System.Collections.ArrayList;
                 if (searchType == SearchType.page)
                 {
                     FacebookEntity result = facebookservice.Search(this.txbQuery.Text, searchType, null) as FacebookEntity;
                     BindResultPage(result);
                 }

            }
        }

        private void BindResultPage(FacebookEntity result) 
        {
            this.listOfSearchResults.DataSource = result.Properties;
            this.listOfSearchResults.DataBind();

            if (!String.IsNullOrWhiteSpace(result.NextPage))
            {
                next.CommandArgument = result.NextPage;
                next.Visible = true;
            }
            else 
            {
                next.Visible = false;
            }

            if (!String.IsNullOrWhiteSpace(result.PreviousPage))
            {
                previous.CommandArgument = result.PreviousPage;
                previous.Visible = true;
            }
            else
            {
                
                previous.Visible = false;
            }
        
        }
    }
}